package com.space.school.controller;

import com.space.school.domain.Category;
import com.space.school.domain.User;
import com.space.school.services.CategoryService;
import com.space.school.services.RegisterService;
import com.space.school.services.UserService;
import com.space.school.services.impl.CategoryServiceImpl;
import com.space.school.services.impl.RegisterServiceImpl;
import com.space.school.services.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "categoryOperateServlet", value = "/categoryOperateServlet")
public class CategoryOperateServlet extends HttpServlet {
    CategoryService categoryService = new CategoryServiceImpl();
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String category_id = req.getParameter("category_id");
        String operate = req.getParameter("operate");
        String update_msg = req.getParameter("update_msg");
        req.getSession().setAttribute("update_msg", update_msg);
//        id的初始值  因为如果是新增条件的情况
//        userId没有赋值,为null 所以 我初始化id为0
        int id = 0;
        if (category_id != null) {
            id = Integer.parseInt(category_id);
        }
//       删除操作
        if (operate.equals("del")) {
            categoryService.delete(id);
            resp.sendRedirect("/categoryServlet");
        } else if (operate.equals("update")) {
//            先把userid 对应的值取出来
            Category category = categoryService.findById(id);

//                往里面防值
            req.setAttribute("category_id", category.getCategory_id());
//            req.getSession().setAttribute("update_msg", "update_msg");
            req.getRequestDispatcher("updateCategoryList.jsp").forward(req, resp);


        } else if (operate.equals("add")) {

            req.getRequestDispatcher("insertCategoryList.jsp").forward(req, resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        设定取出值的编码格式
        req.setCharacterEncoding("UTF-8");
//         req.getParameter  获取参数  Parameter
        String category_name = req.getParameter("category_name");
        String category_type = req.getParameter("category_type");
        String category_id = req.getParameter("category_id");
        boolean flag = categoryService.isInsert(category_name);
        if (category_id == null) {
            if (flag) {
                categoryService.insert(category_name, category_type);
                req.setAttribute("category_name", req.getParameter("category_name"));
                resp.sendRedirect("/categoryServlet");
            } else {

                req.getSession().setAttribute("insert_msg", "商品名称不能重复");
                req.getRequestDispatcher("insertCategoryList.jsp").forward(req, resp);

            }
        } else {
//            修改操作
            Category category = new Category();
            category.setCategory_id(Integer.parseInt(category_id));
            category.setCategory_name(category_name);
            category.setCategory_type(Integer.parseInt(category_type));
            if (flag) {
                categoryService.update(category);
                resp.sendRedirect("/categoryServlet");
                      } else {
//                req.getSession().setAttribute("update_msg", "商品名称不能重复");
//                req.setAttribute("category",category);
//                req.getRequestDispatcher("updateCategoryList.jsp").forward(req, resp);

//                req.getSession().setAttribute("update_msg", "商品名称不能重复");

//                url默认的编码为AssLL码
                resp.sendRedirect("/categoryOperateServlet?category_id=" + category_id + "&category_name=" + java.net.URLEncoder.encode(category_name, "UTF-8")+"&operate=update"+"&update_msg="+java.net.URLEncoder.encode("商品名称不能重复", "UTF-8"));
            }
        }
    }
}

